//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package net.mingsoft.basic.realm;

import java.util.Iterator;
import java.util.List;
import net.mingsoft.base.entity.BaseEntity;
import net.mingsoft.basic.biz.IManagerBiz;
import net.mingsoft.basic.biz.IModelBiz;
import net.mingsoft.basic.entity.ManagerEntity;
import net.mingsoft.basic.entity.ModelEntity;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

public class ManagerAuthRealm extends BaseAuthRealm {
    @Autowired
    private IManagerBiz managerBiz;
    @Autowired
    private IModelBiz modelBiz;

    public ManagerAuthRealm() {
    }

    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        UsernamePasswordToken upToken = (UsernamePasswordToken)token;
        ManagerEntity newManager = new ManagerEntity();
        newManager.setManagerName(upToken.getUsername());
        ManagerEntity manager = (ManagerEntity)this.managerBiz.getEntity(newManager);
        return manager != null ? new SimpleAuthenticationInfo(manager, manager.getManagerPassword(), this.getName()) : null;
    }

    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        ManagerEntity newManager = (ManagerEntity)principalCollection.fromRealm(this.getName()).iterator().next();
        ManagerEntity manager = (ManagerEntity)this.managerBiz.getEntity(newManager);
        if (null == manager) {
            return null;
        } else {
            SimpleAuthorizationInfo result = new SimpleAuthorizationInfo();
            List<ModelEntity> models = this.modelBiz.queryModelByRoleId(manager.getRoleId());
            Iterator var6 = models.iterator();

            while(var6.hasNext()) {
                BaseEntity e = (BaseEntity)var6.next();
                ModelEntity me = (ModelEntity)e;
                if (!StringUtils.isEmpty(me.getModelUrl())) {
                    result.addStringPermission(me.getModelUrl());
                }
            }

            return result;
        }
    }
}
